Skip to content

Conversation

@gennaroprota
Copy link
Contributor

@gennaroprota gennaroprota commented Oct 9, 2025

Reason: Common hygiene for multi-line macros. Makes things like

if (condition)
    BOOST_TEST_THROWS(...);
else
    ...

work correctly. Also, it doesn't generate an empty statement (which compilers may warn about) when the user adds a semicolon (which they usually do).

@pdimov
Copy link
Member

pdimov commented Oct 9, 2025

This is technically a breaking change because the semicolon is now required, whereas it wasn't before. But it looks like all our uses have a semicolon.

The backslash at the last line shouldn't be present. We should remove it from BOOST_TEST_THROWS, instead of adding it to BOOST_TEST_NO_THROW.

@gennaroprota
Copy link
Contributor Author

The backslash at the last line shouldn't be present. We should remove it from BOOST_TEST_THROWS, instead of adding it to BOOST_TEST_NO_THROW.

I thought the convention was to terminate with a // comment:

last_line    \
//

Didn't notice this hadn't been done for BOOST_TEST_NO_THROW().

…EST_NO_THROW()

Reason: Common hygiene for multi-line macros. Makes things like

if (condition)
    BOOST_TEST_THROWS(...);
else
    ...

work correctly. Also, it doesn't generate an empty statement (which
compilers may warn about) when the user adds a semicolon (which they
usually do).
@gennaroprota gennaroprota force-pushed the fix/use_do_while_false_for_boost_test_throws_and_boost_test_no_throw branch from 459d57d to 8a352e2 Compare October 9, 2025 17:01
@pdimov
Copy link
Member

pdimov commented Oct 9, 2025

libs\core\test\lightweight_test_test.cpp(105) : warning C4127: conditional expression is constant

Yeah :-/

@gennaroprota
Copy link
Contributor Author

Sigh. Which version of MSVC? I don't get that warning with MSVC 2022 and /Wall.

@pdimov
Copy link
Member

pdimov commented Oct 10, 2025

Up to msvc-12.0 according to Appveyor.

@gennaroprota
Copy link
Contributor Author

Hmm. We might try with do {} while (0), which is probably more common and might be special-cased, even in old versions of MSVC.

@Lastique
Copy link
Member

Lastique commented Oct 10, 2025

Just add _Pragma("warning(disable: 4127)") to the macros. With the corresponding push and pop pragmas as well.

@gennaroprota gennaroprota force-pushed the fix/use_do_while_false_for_boost_test_throws_and_boost_test_no_throw branch 2 times, most recently from 0cfc658 to d30689c Compare October 13, 2025 17:38
@pdimov
Copy link
Member

pdimov commented Oct 13, 2025

BOOST_TEST_xxx is a public macro name. Please use BOOST_LWT_DETAIL_xxx for implementation details.

@gennaroprota gennaroprota force-pushed the fix/use_do_while_false_for_boost_test_throws_and_boost_test_no_throw branch from d30689c to a931f06 Compare October 13, 2025 18:11
@Lastique
Copy link
Member

Lastique commented Oct 13, 2025

One other alternative we could try is for (;;) { x; break; }.

@gennaroprota
Copy link
Contributor Author

That doesn't play well with the semicolon (the one after the macro invocation).

@gennaroprota gennaroprota force-pushed the fix/use_do_while_false_for_boost_test_throws_and_boost_test_no_throw branch 2 times, most recently from 48268d5 to 7ffb4f8 Compare October 14, 2025 07:30
@gennaroprota gennaroprota force-pushed the fix/use_do_while_false_for_boost_test_throws_and_boost_test_no_throw branch from 7ffb4f8 to 4ee817d Compare October 14, 2025 13:20
@pdimov
Copy link
Member

pdimov commented Oct 14, 2025

Everything fails, probably because you're missing a semicolon here BOOST_LWT_DETAIL_DO_WHILE_FALSE(static_cast<void>(0)).

@pdimov
Copy link
Member

pdimov commented Oct 14, 2025

(void)0; is probably a more idiomatic form of a null statement, despite the C cast.

@gennaroprota gennaroprota force-pushed the fix/use_do_while_false_for_boost_test_throws_and_boost_test_no_throw branch from 4ee817d to 1ed1e04 Compare October 14, 2025 13:29
@gennaroprota
Copy link
Contributor Author

Do you prefer that? (Why?)

@pdimov
Copy link
Member

pdimov commented Oct 14, 2025

It's idiomatic because that's what assert typically uses.

Nowadays (void) is also emerging as the idiomatic way to silence nodiscard warnings.

@gennaroprota
Copy link
Contributor Author

I don't have a strong opinion about that. I can change it, if you like. (About assert(), though, I think it's (void) because it's common to C.)

@pdimov
Copy link
Member

pdimov commented Oct 14, 2025

Yes please, let's go with (void). I've always found static_cast<void> somewhat pretentious, and it does require parentheses around the expression which is also a bit jarring.

@gennaroprota gennaroprota force-pushed the fix/use_do_while_false_for_boost_test_throws_and_boost_test_no_throw branch from 1ed1e04 to 40fda50 Compare October 15, 2025 10:31
@pdimov
Copy link
Member

pdimov commented Oct 15, 2025

@Lastique @glenfe Any objections to merging this?

@Lastique
Copy link
Member

No, looks good to me.

@pdimov pdimov merged commit 6814bc5 into boostorg:develop Oct 18, 2025
72 checks passed
@pdimov
Copy link
Member

pdimov commented Oct 18, 2025

@pdimov
Copy link
Member

pdimov commented Oct 18, 2025

Looks like this is the only such occurrence though.

pdimov added a commit to boostorg/unordered that referenced this pull request Oct 18, 2025
pdimov added a commit to boostorg/variant2 that referenced this pull request Oct 18, 2025
@gennaroprota
Copy link
Contributor Author

gennaroprota commented Oct 20, 2025

@Lastique: That changelog entry is a bit wrong: With the previous implementation (without do {} while (false)), this:

if (condition)
    BOOST_TEST_THROWS(...); // note the semicolon
else
    ...

was a syntax error. The warnings you hint at, instead, were for the do {} while (false) itself.

@Lastique
Copy link
Member

The previous definition was a direct try/catch, so if I'm not mistaken, it wasn't an error under if without the semicolon, was it? BOOST_TEST_NO_THROW also wasn't an error.

BTW, given that the macros are now conditionally defined depending on BOOST_MSVC, does this mean that the motivating example with if/else and semicolon is still not portable and should not be used despite this PR?

Anyway, if you can provide a better text in the changelog, please create a PR.

@gennaroprota
Copy link
Contributor Author

gennaroprota commented Oct 20, 2025

The previous definition was a direct try/catch, so if I'm not mistaken, it wasn't an error under if without the semicolon, was it? BOOST_TEST_NO_THROW also wasn't an error.

Yes.

BTW, given that the macros are now conditionally defined depending on BOOST_MSVC, does this mean that the motivating example with if/else and semicolon is still not portable and should not be used despite this PR?

Yes, it can be used only if MSVC < 2015 is of no concern :-(. That MSVC warning ruined everything. I wish I could withdraw this PR.

@pdimov
Copy link
Member

pdimov commented Oct 20, 2025

Fixed in 7b2c714.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants